package com.example.swaggerDemo.service;
import com.example.swaggerDemo.Dao.ArticleDao;
import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.MsgEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * JdbcTemplate方式完成对Article表的操作
 * @author chenyong
 * 2019年5月13日上午
 */
//作用于持久层.用来表明该类是用来执行与数据库相关的操作（即dao对象），并支持自动处理数据库操作产生的异常
@Repository
public class ArticleService implements ArticleDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    @Override
    public   List<ArticleEntity> findAll(   @RequestParam("pageindex") int pagindex,
                                            @RequestParam("pagesize") int pagesize) throws SQLException {

        String sql = "select * from Article"+ " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
                ;
        List<ArticleEntity> dto= jdbcTemplate.query(sql, new BeanPropertyRowMapper(ArticleEntity.class));
        return dto;
    }

    @Override
    public  ArticleEntity findid(@RequestParam("id") String id) throws SQLException {
           String sql = "select * from Article WHERE  id= '" +  id + "'";
           ArticleEntity dto= jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<ArticleEntity>(ArticleEntity.class));
         return dto;
    }

    /**
     * 修改阅读数量
     * @param readcount
     * @param id
     * @return
     */
    @Override
    public int updateReadcountl(@RequestParam("readcount") int readcount,
                                @RequestParam("id") String id) {
        String sql = "update  Article set readcount=? where id=?";
        return jdbcTemplate.update(sql,readcount,id);
    }

    /**
     * 添加广告
     * @param title
     * @param pretext
     * @param preimage
     * @param content
     * @return
     * @throws SQLException
     */
    @Override
    public int inserarticle(@RequestParam("title") String title,
                            @RequestParam("pretext") String pretext,
                            @RequestParam("preimage") String preimage,
                            @RequestParam("content") String content) throws SQLException {
        String createtime= df.format(new Date());
        String status="1";
        String sql = "insert into article (title,pretext,preimage,content,createtime,status,readcount) " +
                "values(?,?,?,?,?,?,?)";

         return jdbcTemplate.update(sql,title,pretext,preimage,content,createtime,status,0);
    }

    /**
     * 修改广告
     * @param id
     * @param title
     * @param pretext
     * @param preimage
     * @param content
     * @return
     */
    @Override
    public int update(
            @RequestParam("id") String id,
            @RequestParam("title") String title,
                      @RequestParam("pretext") String pretext,
                      @RequestParam("preimage") String preimage,
                      @RequestParam("content") String content) {
        String sql = "update  article set title=?,pretext=?,preimage=?,content=? where id=?";
        return jdbcTemplate.update(sql,title,pretext,preimage,content,id);
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @Override
    public int delete(@RequestParam("id") String id) {
        String sql = "delete from article  where id=?";
        return jdbcTemplate.update(sql,id);
    }
}
